Categories
JavaScript Basics

Highlights of JavaScript — Alerts, Variables, Numbers, and Strings

Spread the love

To learn JavaScript, we must learn the basics.

In this article, we’ll look at the most basic parts of the JavaScript language.

Alerts

The alert function displays an alert box with some text we pass in.

The text comes from the string argument.

For instance, can write:

alert("hello world!");

and ‘hello world’ would be displayed.

It is a global function, so it’s a property of window . window.alert is the same as alert .

The short form is fine for inclusion in our code.

Variables and Strings

Strings are basic elements of JavaScript code. They store text data.

A variable with the given name can only be declared once.

They can be stored in variables. Variables are names that we choose and assign values to so that we can store them for use later.

For example, we can write:

let name = "james";

to store names.

We can set a variable declared with let to a new value.

For example, we can write:

let name = "james";
name = "mark";

Then the value of name will be "mark" .

We can declare a variable and leave it undefined.

For instance, we can write:

let name;
name = "james";

We can do this with variables declared with let .

Variables can be declared with any name that isn’t in the reserved keywords list.

The list is at https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#Keywords.

Variables aren’t enclosed in quotes and text strings are enclosed in quotes.

JavaScript strings can be enclosed in single or double-quotes.

They can also be enclosed with backticks.

If they’re enclosed with backticks, then they have to have variables embedded in them.

For example, we can write:

let firstName = 'james';
let lastName = 'smith';
let name = `${firstName} ${lastName}`;

We interpolate the value of firstName and lastName into the string. So name should be 'james smith' .

This is a template literal. We can put any expression inside the strings.

This is a very convenient way to combine various expressions into a string.

Now we can assign variables, we can pass variables into alert instead of passing the whole string.

For example, we can write:

let text = "hello world!"
alert(text);

We pass in text into alert as an argument to display the alert.

We can reference text anywhere after it’s defined.

Variables and Numbers

Numbers are another primitive data type with JavaScript.

We can also assign them to variables.

For example, we can write:

let length = 100;

This makes doing operations on it easy. For example, we can add a number to length :

length + 10

then the sum of length and 10 is 110.

We know that length isn’t a variable because it’s not enclosed in quotes.

And numbers alone can be used variable names, so JavaScript knows it’s a number.

We can replace 10 with a variable by assigning it to a variable.

For example, we can write:

let length = 100;
let additionalLength = 10;
let totalLength = length + additionaLength;

We have the additionalLength variable that we add to the length and assign the sum to the totalLength variable.

Conclusion

We can create variables to store numbers and strings and use them with alerts.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *